<?xml version="1.0"?>
<yandex>
  <!-- config.xml引用的名字 -->
  <!-- 集群配置（动态生成2分片2副本） -->
  <clickhouse_remote_servers>
    <{{ ck_cluster_name }}>
      <!-- 分片1（自动选择前2台作为副本） -->
      <shard>
        <internal_replication>false</internal_replication>
        {% for host in groups['clickhouse'][0:2] %}
        <replica>
          <host>{{ hostvars[host].ansible_host }}</host>
          <port>{{ ck_tcp_port }}</port>
          <user>default</user>
          <password>{{ ck_default_password }}</password>
        </replica>
        {% endfor %}
      </shard>

      <!-- 分片2（自动选择中2台作为副本） -->
      <shard>
        <internal_replication>false</internal_replication>
        {% for host in groups['clickhouse'][2:4] %}
        <replica>
          <host>{{ hostvars[host].ansible_host }}</host>
          <port>{{ ck_tcp_port }}</port>
          <user>default</user>
          <password>{{ ck_default_password }}</password>
        </replica>
        {% endfor %}
      </shard>

      <!-- 分片3（自动选择后2台作为副本） -->
      <shard>
        <internal_replication>false</internal_replication>
        {% for host in groups['clickhouse'][4:6] %}
        <replica>
          <host>{{ hostvars[host].ansible_host }}</host>
          <port>{{ ck_tcp_port }}</port>
          <user>default</user>
          <password>{{ ck_default_password }}</password>
        </replica>
        {% endfor %}
      </shard>
    </{{ ck_cluster_name }}>
  </clickhouse_remote_servers>

  <!-- 使用集群的时候需要配置zookeeper，用于副本数据同步 -->
  <!-- 动态Keeper配置 -->
  <zookeeper-servers>
  {% for host in groups['clickhouse'] if hostvars[host].keeper_id is defined %}
  <node index="{{ hostvars[host].keeper_id }}">  <!-- 直接用keeper_id作为index -->
    <host>{{ hostvars[host].ansible_host }}</host>
    <port>{{ ck_keeper_port | default(9181) }}</port>
  </node>
  {% endfor %}
  </zookeeper-servers>

  <!-- macros配置 -->
  <macros>
    <shard>{{ shard_id }}</shard>
    <!-- 每个节点使用自己的IP作为副本标识 -->
    <replica>{{ ansible_host }}</replica>
  </macros>

  <networks>
    <ip>::/0</ip>
  </networks>

  <!-- 会压缩30-100%的数据，节省磁盘空间 -->
  <clickhouse_compression>
    <case>
      <!-- 最小的part -->
      <min_part_size>10000000000</min_part_size>
      <!-- 整个表里最小的part -->
      <min_part_size_ratio>0.01</min_part_size_ratio>
      <!-- 压缩方式. -->
      <method>lz4</method>
    </case>
  </clickhouse_compression>
</yandex>
